package com.qs.eggs.utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;


import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Tools {


	public static boolean  isNetworkConnected(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
		NetworkInfo info = cm.getActiveNetworkInfo();
		return info != null && info.isConnected();
	}

	/**
	 * 格式化字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String formatString(String resultStr) {
		if (resultStr != null) {
			Pattern p = Pattern.compile("\t|\r|\n");
			Matcher m = p.matcher(resultStr);
			resultStr = m.replaceAll("");
		}

		if (resultStr.startsWith("null(\"")) {

			resultStr = (String) resultStr.subSequence(6,
					resultStr.length() - 2);
		}

		if (resultStr.startsWith("null(")) {
			resultStr = (String) resultStr.subSequence(5,
					resultStr.length() - 1);
		}

		if (resultStr.startsWith("\"")) {
			resultStr = (String) resultStr.subSequence(1,
					resultStr.length() - 1);
		}

		resultStr = resultStr.replaceAll("\\\\n", "\n");
		// 去除反斜线转�?
		resultStr = resultStr.replaceAll("\\\\", "");

		// 规范数组字符�?
		resultStr = resultStr.replaceAll("\"\\[", "[");
		resultStr = resultStr.replaceAll("\\]\"", "]");

		resultStr = resultStr.replaceAll("\"\\{", "{");
		resultStr = resultStr.replaceAll("\\}\"", "}");

		resultStr = resultStr.replaceAll("<br/>", "\n");

		System.out.println(">>>>>>>这是返回的数�?>>>>>>>>" + resultStr);

		return resultStr;

	}

	/**
	 * 格式化字符串
	 *
	 */
	public static String formatString2(String resultStr) {
		if (resultStr != null) {
			Pattern p = Pattern.compile("\t|\r|\n");
			Matcher m = p.matcher(resultStr);
			resultStr = m.replaceAll("");
		}
		resultStr = resultStr.replaceAll("\"", "�?");
		resultStr = resultStr.replaceAll("\'", "�?");
		// 规范数组字符�?
		resultStr = resultStr.replaceAll("\"\\[", "[");
		resultStr = resultStr.replaceAll("\\]\"", "]");

		resultStr = resultStr.replaceAll("\\\\n", "\n");
		// 去除反斜线转�?
		resultStr = resultStr.replaceAll("\\\\", "");
		return resultStr;
	}

	/**
	 * 得到返回的页�?
	 *
	 */
	public static int getPageNum(int total, int row) {

		return total % row == 0 ? total / row : total / row + 1;

	}

	/**
	 * 得到2014-12-16 00:00:00字符串的日期
	 *
	 */
	public static String getDate(String Str) {
		if (Str == null) {
			return "";
		}
		if (Str.length() > 10) {
			return Str.substring(0, 10);
		}
		return Str;

	}

	/**
	 * 得到2014-12-16 00:00:00字符串的时间
	 *
	 *            -12-16 00:00:00
	 * @return 00:00:00
	 */
	public static String getTime(String Str) {

		if (Str == null) {
			return "";
		}

		if (Str.length() > 10) {
			return Str.substring(10);
		}
		return Str;

	}

	/**
	 * 得到当前时间yyyy-MM-dd
	 * 
	 * @param thisMonth
	 *            false 表示上一�?
	 * @return
	 */
	public static String getThisTime(boolean thisMonth) {
		Calendar c = Calendar.getInstance();

		DateFormat df = new SimpleDateFormat("yyyy-M-d");

		if (!thisMonth) {
			c.add(Calendar.MONTH, -1); // 目前时间减一个月
		}

		return df.format(c.getTime());
	}

	/**
	 * 得到当前时间yyyy-MM-dd HH:mm:ss
	 *
	 */
	public static String getThisTime() {
		Calendar c = Calendar.getInstance();

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		return df.format(c.getTime());
	}
	
	/**
	 * 得到当前时间yyyy-MM-dd HH:mm:ss
	 *
	 */
	public static String getThisDay() {
		Calendar c = Calendar.getInstance();

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

		return df.format(c.getTime());
	}

	/**
	 * 在ArrayList<HashMap<String, String>>查找是否包含传入的字符串,存在返回索引,不存在返�?0
	 * 
	 * @param data
	 * @param str
	 * @return
	 */
	public static int getIndexOfList(ArrayList<HashMap<String, String>> data,
			String str) {

		if (str == null) {
			return 0;
		} else if (str.length() > 2) {
			str = str.substring(0, 2);

		}
		System.out.println("这是截取的字符串>>>" + str);

		for (int i = 0; i < data.size(); i++) {
			HashMap<String, String> map = data.get(i);
			if (map.get("name").contains(str)) {
				return i;
			}
		}
		return 0;
	}

	/**
	 * 拨打电话
	 * 
	 * @param context
	 * @param telnum
	 */
	public static void calltocus(Context context, String telnum) {
		String num = telnum.trim();
		// Intent intent = new Intent();
		// // 向intent里写入意�?
		// intent.setAction(Intent.ACTION_CALL);
		// // 设置要拨打的电话号码�?"tel:" + num，tel+号码
		// intent.setData(Uri.parse("tel:" + num));
		// // 另外在配置里是要申请权限�?
		// context.startActivity(intent);

		Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + num));
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);

	}



	/**
	 * 成功回调方法的接�?
	 */
	public static interface onItemSelectListener {
		void onSuccess(String result);

		void onFail(String result);
	}

	/**
	 * Doule-->String：保留两位小�?
	 * @param d
	 * @return
	 */
	public static String getDouble(double d){
		String str = "";
		DecimalFormat df = new DecimalFormat("0.00");
		str = df.format(d);
		return str;
	}	
	/**
	 * Double-->String：不保留小数
	 * @param d
	 * @return
	 */
	public static String getDouble2(double d){
		String str = "";
		DecimalFormat df = new DecimalFormat("0");
		str = df.format(d);
		return str;
	}

	/**
	 * 不保留小数位
	 * @param d
	 * @return
	 */
	public static int getInt(String d){
		
		String str = "";
		DecimalFormat df = new DecimalFormat("0");
		str = df.format(Double.parseDouble(d));
		return Integer.parseInt(str);
	}

}
